/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.zx.barbecue.signup.web;

import java.util.Date;

import javax.jms.Queue;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.app.util.wx.SocketMessage;
import com.jeesite.modules.zx.barbecue.signup.entity.TbarbecueSignup;
import com.jeesite.modules.zx.barbecue.signup.service.TbarbecueSignupService;
import com.jeesite.modules.zx.sys.msg.push.entity.ZxMsgPush;
import com.jeesite.modules.zx.sys.msg.push.service.ZxMsgPushService;

/**
 * 考试报名表Controller
 * @author zpp
 * @version 2020-01-19
 */
@Controller
@RequestMapping(value = "${adminPath}/zx/barbecue/signup/tbarbecueSignup")
public class TbarbecueSignupController extends BaseController {

	@Autowired
	private TbarbecueSignupService tbarbecueSignupService;
	@Autowired
	private ZxMsgPushService zxMsgPushService;
	@Autowired
    private Queue queue;
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TbarbecueSignup get(String id, boolean isNewRecord) {
		return tbarbecueSignupService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("zx:barbecue:signup:tbarbecueSignup:view")
	@RequestMapping(value = {"list", ""})
	public String list(TbarbecueSignup tbarbecueSignup, Model model) {
		model.addAttribute("tbarbecueSignup", tbarbecueSignup);
		return "modules/zx/barbecue/signup/tbarbecueSignupList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("zx:barbecue:signup:tbarbecueSignup:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TbarbecueSignup> listData(TbarbecueSignup tbarbecueSignup, HttpServletRequest request, HttpServletResponse response) {
		tbarbecueSignup.setPage(new Page<>(request, response));
		Page<TbarbecueSignup> page = tbarbecueSignupService.findPage(tbarbecueSignup);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("zx:barbecue:signup:tbarbecueSignup:view")
	@RequestMapping(value = "form")
	public String form(TbarbecueSignup tbarbecueSignup, Model model) {
		model.addAttribute("tbarbecueSignup", tbarbecueSignup);
		return "modules/zx/barbecue/signup/tbarbecueSignupForm";
	}

	/**
	 * 保存考试报名表
	 */
	@RequiresPermissions("zx:barbecue:signup:tbarbecueSignup:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TbarbecueSignup tbarbecueSignup) {
		tbarbecueSignupService.save(tbarbecueSignup);
		return renderResult(Global.TRUE, text("保存考试报名表成功！"));
	}
	
	/**
	 * 停用考试报名表
	 */
//	@RequiresPermissions("zx:barbecue:signup:tbarbecueSignup:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(TbarbecueSignup tbarbecueSignup) {
		tbarbecueSignup.setState(2);
		tbarbecueSignupService.update(tbarbecueSignup);
		return renderResult(Global.TRUE, text("成功"));
	}
	
	/**
	 * 启用考试报名表
	 */
//	@RequiresPermissions("zx:barbecue:signup:tbarbecueSignup:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(TbarbecueSignup tbarbecueSignup) {
		tbarbecueSignup.setState(1);
		tbarbecueSignupService.update(tbarbecueSignup);
		try {
			ZxMsgPush zxMsgPush=new ZxMsgPush();
			zxMsgPush.setSendDate(new Date());
			zxMsgPush.setReceiveCode(tbarbecueSignup.getUserId());
			zxMsgPush.setMsgTitle("考试报名");
			zxMsgPush.setMsgContent("您所提交的<"+tbarbecueSignup.getBarbecueTitle()+">考试报名信息通过审核");
			zxMsgPush.setReadStatus("0");
			zxMsgPushService.save(zxMsgPush);
			String sendUserCode = zxMsgPush.getReceiveCode();
			 ZxMsgPush entity=new ZxMsgPush();
			 entity.setReceiveCode(sendUserCode);
			 entity.setReadStatus("0");
			long findCount = zxMsgPushService.findCount(entity);
			SocketMessage sm=new SocketMessage();
			sm.setType("courseinfo");
			sm.setNum(findCount);
			sm.setUserId(sendUserCode);
			jmsMessagingTemplate.convertAndSend(queue,JsonMapper.toJson(sm));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return renderResult(Global.TRUE, text("成功"));
	}
	
	/**
	 * 删除考试报名表
	 */
	@RequiresPermissions("zx:barbecue:signup:tbarbecueSignup:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TbarbecueSignup tbarbecueSignup) {
		tbarbecueSignupService.delete(tbarbecueSignup);
		return renderResult(Global.TRUE, text("删除考试报名表成功！"));
	}
	
}